﻿using System;
using System.Reflection;

namespace Bamboo.Helpers
{
    public class EnumDescriptionAttribute : Attribute
    {
        private string description;

        public EnumDescriptionAttribute(string description)
        {
            this.description = description;
        }

        public string Description
        {
            get { return this.description; }
        }
    }

    public static class EnumDescriptionExtensions
    {        
        public static string GetEnumDescription(this Enum @enum)
        {
            FieldInfo fieldInfo = @enum.GetType().GetField(@enum.ToString());

            EnumDescriptionAttribute[] attributes = (EnumDescriptionAttribute[]) fieldInfo.GetCustomAttributes(typeof(EnumDescriptionAttribute), false);

            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }

            return String.Empty;
        }
    }
}
